package com.guan.study.utils;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.List;

/**
 * @Date: 2023/7/19 19:35
 * @Author: yaobai
 * @Description: excel自定义合并行
 */
public class CustomMergeColumn implements CellWriteHandler {

    private final List<String> megerColumns;

    public CustomMergeColumn(List<String> megerColumns) {
        this.megerColumns = megerColumns;
    }


    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        if (isHead){
            return;
        }
        if (relativeRowIndex == 0){
            return;
        }

        if (!this.megerColumns.contains(head.getFieldName())){
            return;
        }

        Sheet sheet = cell.getSheet();
        //当前行坐标
        int currentRowIndex = cell.getRowIndex();
        //上一行坐标
        int preRowIndex = currentRowIndex - 1;
        //上一行
        Row preRow = sheet.getRow(preRowIndex);
        //上一个单元格
        Cell preCell = preRow.getCell(cell.getColumnIndex());

        String currentCellValue = "";

        if (cell.getCellType() == CellType.STRING){
            currentCellValue = cell.getStringCellValue();
        } else if (cell.getCellType() == CellType.NUMERIC) {
            currentCellValue = String.valueOf(cell.getDateCellValue());
        }

        String preCellValue = "";

        if (preCell.getCellType() == CellType.STRING){
            preCellValue = preCell.getStringCellValue();
        } else if (preCell.getCellType() == CellType.NUMERIC) {
            preCellValue = String.valueOf(preCell.getDateCellValue());
        }

        if (!currentCellValue.trim().equals(preCellValue.trim())){
            return;
        }

        boolean merge = false;
        List<CellRangeAddress> mergedRegion = sheet.getMergedRegions();
        for (int i = 0; i < mergedRegion.size(); i++) {
            CellRangeAddress addr = mergedRegion.get(i);
            if (addr.isInRange(preRowIndex,cell.getColumnIndex())){
                sheet.removeMergedRegion(i);
                addr.setLastRow(currentRowIndex);
                sheet.addMergedRegion(addr);
                merge = true;
                break;
            }
        }

        if (!merge){
            CellRangeAddress addresses =
                    new CellRangeAddress(preRowIndex,currentRowIndex,cell.getColumnIndex(),cell.getColumnIndex());
            sheet.addMergedRegion(addresses);

        }
    }
}
